/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.zimbra.common.account.ZAttrProvisioning.AccountStatus;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.localconfig.LC.PUBLIC_SHARE_VISIBILITY;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.PublishedShareInfoVisitor;
/*     */ import com.zimbra.cs.account.ShareInfo.Discover;
/*     */ import com.zimbra.cs.account.ShareInfo.MountedFolders;
/*     */ import com.zimbra.cs.account.ShareInfo.Published;
/*     */ import com.zimbra.cs.account.ShareInfoData;
/*     */ import com.zimbra.cs.account.names.NameUtil.EmailAddress;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.account.message.GetShareInfoRequest;
/*     */ import com.zimbra.soap.account.message.GetShareInfoResponse;
/*     */ import com.zimbra.soap.base.GetShareInfoResponseInterface;
/*     */ import com.zimbra.soap.type.AccountSelector;
/*     */ import com.zimbra.soap.type.GranteeChooser;
/*     */ import java.util.Comparator;
/*     */ import java.util.Map;
/*     */ import java.util.SortedSet;
/*     */ import java.util.TreeSet;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetShareInfo
/*     */   extends AccountDocumentHandler
/*     */ {
/*     */   protected Element proxyIfNecessary(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  51 */     if (isInternal(request))
/*     */     {
/*     */ 
/*     */ 
/*  55 */       return null;
/*     */     }
/*     */     
/*     */ 
/*  59 */     return super.proxyIfNecessary(request, context);
/*     */   }
/*     */   
/*     */   private boolean isInternal(Element request) throws ServiceException
/*     */   {
/*  64 */     return request.getAttributeBool("internal", false);
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  70 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  71 */     Account account = getRequestedAccount(zsc);
/*     */     
/*  73 */     if (!canAccessAccount(zsc, account)) {
/*  74 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*     */     
/*  77 */     GetShareInfoResponse response = new GetShareInfoResponse();
/*  78 */     doGetShareInfo(zsc, context, account, request, response);
/*     */     
/*  80 */     return zsc.jaxbToElement(response);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void doGetShareInfo(ZimbraSoapContext zsc, Map<String, Object> context, Account targetAcct, Element request, GetShareInfoResponse response)
/*     */     throws ServiceException
/*     */   {
/*  92 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  94 */     GetShareInfoRequest req = (GetShareInfoRequest)JaxbUtil.elementToJaxb(request);
/*  95 */     GranteeChooser granteeChooser = req.getGrantee();
/*  96 */     byte granteeType = getGranteeType(granteeChooser);
/*  97 */     String granteeId = granteeChooser == null ? null : granteeChooser.getId();
/*  98 */     String granteeName = granteeChooser == null ? null : granteeChooser.getName();
/*     */     
/* 100 */     Account owner = null;
/* 101 */     AccountSelector ownerSelector = req.getOwner();
/* 102 */     if (ownerSelector != null) {
/* 103 */       owner = prov.get(ownerSelector);
/*     */       
/*     */ 
/*     */ 
/* 107 */       if ((owner == null) || (owner.isAccountExternal())) {
/* 108 */         return;
/*     */       }
/* 110 */       ZAttrProvisioning.AccountStatus status = owner.getAccountStatus();
/* 111 */       if ((status != null) && (status.isClosed())) {
/* 112 */         return;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 117 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*     */ 
/* 120 */     ShareInfo.MountedFolders mountedFolders = null;
/* 121 */     if (!Boolean.TRUE.equals(req.getInternal()))
/*     */     {
/*     */ 
/*     */ 
/* 125 */       mountedFolders = new ShareInfo.MountedFolders(octxt, targetAcct);
/*     */     }
/*     */     ResultFilter resultFilter;
/*     */     ResultFilter resultFilter;
/* 129 */     if (Boolean.FALSE.equals(req.getIncludeSelf())) {
/* 130 */       resultFilter = new ResultFilterByTargetExcludeSelf(granteeId, granteeName, targetAcct);
/*     */     } else {
/* 132 */       resultFilter = new ResultFilterByTarget(granteeId, granteeName);
/*     */     }
/*     */     
/* 135 */     String filterDomain = null;
/* 136 */     if (LC.PUBLIC_SHARE_VISIBILITY.samePrimaryDomain.equals(LC.getPublicShareAdvertisingScope())) {
/* 137 */       filterDomain = targetAcct.getDomainName();
/*     */     }
/* 139 */     ResultFilter resultFilter2 = new ResultFilterForPublicShares(filterDomain);
/*     */     
/* 141 */     ShareInfoVisitor visitor = new ShareInfoVisitor(prov, response, mountedFolders, resultFilter, resultFilter2);
/*     */     
/* 143 */     if (owner == null)
/*     */     {
/* 145 */       ShareInfo.Published.get(prov, targetAcct, granteeType, owner, visitor);
/*     */ 
/*     */     }
/*     */     else
/*     */     {
/*     */ 
/* 151 */       if (targetAcct.getId().equals(owner.getId())) {
/* 152 */         throw ServiceException.INVALID_REQUEST("cannot discover shares on self", null);
/*     */       }
/* 154 */       if (Provisioning.onLocalServer(owner)) {
/* 155 */         ShareInfo.Discover.discover(octxt, prov, targetAcct, granteeType, owner, visitor);
/*     */       }
/*     */       else
/*     */       {
/* 159 */         fetchRemoteShareInfo(context, request, owner.getId(), visitor);
/*     */       }
/*     */     }
/*     */     
/* 163 */     visitor.finish();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void fetchRemoteShareInfo(Map<String, Object> context, Element request, String ownerId, ShareInfoVisitor visitor)
/*     */     throws ServiceException
/*     */   {
/* 185 */     request.addAttribute("internal", true);
/*     */     
/* 187 */     Element response = proxyRequest(request, context, getServer(ownerId));
/* 188 */     for (Element eShare : response.listElements("share")) {
/* 189 */       ShareInfoData sid = ShareInfoData.fromXML(eShare);
/* 190 */       visitor.visit(sid);
/*     */     }
/*     */   }
/*     */   
/*     */   public static byte getGranteeType(GranteeChooser granteeChooser) throws ServiceException {
/* 195 */     String granteeType = null;
/* 196 */     if (granteeChooser != null) {
/* 197 */       granteeType = granteeChooser.getType();
/*     */     }
/* 199 */     return getGranteeType(granteeType);
/*     */   }
/*     */   
/*     */   public static byte getGranteeType(String granteeType) throws ServiceException {
/* 203 */     return granteeType == null ? 0 : ACL.stringToType(granteeType);
/*     */   }
/*     */   
/*     */   public static abstract interface ResultFilter
/*     */   {
/*     */     public abstract boolean check(ShareInfoData paramShareInfoData);
/*     */   }
/*     */   
/*     */   public static class ResultFilterByTarget implements GetShareInfo.ResultFilter
/*     */   {
/*     */     String mGranteeId;
/*     */     String mGranteeName;
/*     */     
/*     */     public ResultFilterByTarget(String granteeId, String granteeName)
/*     */     {
/* 218 */       this.mGranteeId = granteeId;
/* 219 */       this.mGranteeName = granteeName;
/*     */     }
/*     */     
/*     */     public boolean check(ShareInfoData sid)
/*     */     {
/* 224 */       if ((this.mGranteeId != null) && (!this.mGranteeId.equals(sid.getGranteeId()))) {
/* 225 */         return false;
/*     */       }
/* 227 */       if ((this.mGranteeName != null) && (!this.mGranteeName.equals(sid.getGranteeName()))) {
/* 228 */         return false;
/*     */       }
/* 230 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ResultFilterByTargetExcludeSelf extends GetShareInfo.ResultFilterByTarget {
/*     */     Account mSelf;
/*     */     
/*     */     public ResultFilterByTargetExcludeSelf(String granteeId, String granteeName, Account self) {
/* 238 */       super(granteeName);
/* 239 */       this.mSelf = self;
/*     */     }
/*     */     
/*     */     public boolean check(ShareInfoData sid)
/*     */     {
/* 244 */       if (this.mSelf.getId().equals(sid.getOwnerAcctId())) {
/* 245 */         return false;
/*     */       }
/* 247 */       return super.check(sid);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ResultFilterForPublicShares
/*     */     implements GetShareInfo.ResultFilter
/*     */   {
/*     */     private final String ownerDomainFilter;
/*     */     private final boolean hideAllPublicShares;
/*     */     
/*     */     public ResultFilterForPublicShares(String ownerDomainFilter)
/*     */     {
/* 259 */       this.ownerDomainFilter = ownerDomainFilter;
/* 260 */       this.hideAllPublicShares = LC.PUBLIC_SHARE_VISIBILITY.none.equals(LC.getPublicShareAdvertisingScope());
/*     */     }
/*     */     
/*     */ 
/*     */     public boolean check(ShareInfoData sid)
/*     */     {
/* 266 */       if (6 != sid.getGranteeTypeCode()) {
/* 267 */         return true;
/*     */       }
/* 269 */       if (this.hideAllPublicShares) {
/* 270 */         ZimbraLog.misc.debug("Skipping unmounted public share '%s'", new Object[] { sid.getName() });
/* 271 */         return false;
/*     */       }
/* 273 */       if (this.ownerDomainFilter == null) {
/* 274 */         return true;
/*     */       }
/*     */       try
/*     */       {
/* 278 */         String ownerDomain = NameUtil.EmailAddress.getDomainNameFromEmail(sid.getOwnerAcctEmail());
/* 279 */         if (this.ownerDomainFilter.equalsIgnoreCase(ownerDomain)) {
/* 280 */           return true;
/*     */         }
/* 282 */         ZimbraLog.misc.debug("Skipping public share '%s' - owner domain '%s' is not the same as '%s'", new Object[] { sid.getName(), ownerDomain, this.ownerDomainFilter });
/*     */       }
/*     */       catch (ServiceException e)
/*     */       {
/* 286 */         ZimbraLog.misc.debug("Problem checking domain of share owner '%s'", sid.getOwnerAcctEmail(), e);
/*     */       }
/* 288 */       return false;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ShareInfoVisitor implements Provisioning.PublishedShareInfoVisitor {
/*     */     Provisioning mProv;
/*     */     GetShareInfoResponseInterface mResp;
/*     */     ShareInfo.MountedFolders mMountedFolders;
/*     */     GetShareInfo.ResultFilter mResultFilter;
/*     */     GetShareInfo.ResultFilter resultFilterForUnmounted;
/* 298 */     SortedSet<ShareInfoData> mSortedShareInfo = new TreeSet(new ShareInfoComparator(null));
/*     */     
/*     */     private static class ShareInfoComparator implements Comparator<ShareInfoData>
/*     */     {
/*     */       public int compare(ShareInfoData a, ShareInfoData b) {
/* 303 */         int r = a.getPath().compareToIgnoreCase(b.getPath());
/* 304 */         if (r == 0) {
/* 305 */           r = a.getOwnerAcctEmail().compareToIgnoreCase(b.getOwnerAcctEmail());
/*     */         }
/* 307 */         if (r == 0) {
/* 308 */           if ((a.getGranteeName() != null) && (b.getGranteeName() != null)) {
/* 309 */             r = a.getGranteeName().compareToIgnoreCase(b.getGranteeName());
/* 310 */           } else if (a.getGranteeName() == null) {
/* 311 */             r = b.getGranteeName() == null ? 0 : -1;
/*     */           } else {
/* 313 */             r = 1;
/*     */           }
/*     */         }
/* 316 */         if (r == 0) {
/* 317 */           r = a.getGranteeType().compareTo(b.getGranteeType());
/*     */         }
/* 319 */         return r;
/*     */       }
/*     */     }
/*     */     
/*     */     public ShareInfoVisitor(Provisioning prov, GetShareInfoResponseInterface resp, ShareInfo.MountedFolders mountedFolders, GetShareInfo.ResultFilter resultFilter)
/*     */     {
/* 325 */       this(prov, resp, mountedFolders, resultFilter, null);
/*     */     }
/*     */     
/*     */ 
/*     */     public ShareInfoVisitor(Provisioning prov, GetShareInfoResponseInterface resp, ShareInfo.MountedFolders mountedFolders, GetShareInfo.ResultFilter resultFilter, GetShareInfo.ResultFilter resultFilter2)
/*     */     {
/* 331 */       this.mProv = prov;
/* 332 */       this.mResp = resp;
/* 333 */       this.mMountedFolders = mountedFolders;
/* 334 */       this.mResultFilter = resultFilter;
/* 335 */       this.resultFilterForUnmounted = resultFilter2;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     public void visit(ShareInfoData sid)
/*     */       throws ServiceException
/*     */     {
/* 343 */       if ((this.mResultFilter == null) || (this.mResultFilter.check(sid)))
/*     */       {
/* 345 */         if ((this.resultFilterForUnmounted == null) || (getMountpointId(sid) != null) || (this.resultFilterForUnmounted.check(sid)))
/*     */         {
/* 347 */           this.mSortedShareInfo.add(sid);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public void finish() throws ServiceException {
/* 353 */       for (ShareInfoData sid : this.mSortedShareInfo) {
/* 354 */         doVisit(sid);
/*     */       }
/*     */     }
/*     */     
/*     */     private void doVisit(ShareInfoData sid)
/*     */       throws ServiceException
/*     */     {
/* 361 */       this.mResp.addShare(sid.toJAXB(getMountpointId(sid)));
/*     */     }
/*     */     
/*     */     private Integer getMountpointId(ShareInfoData sid) throws ServiceException {
/* 365 */       Integer mptId = null;
/* 366 */       if (this.mMountedFolders != null) {
/* 367 */         mptId = this.mMountedFolders.getLocalFolderId(sid.getOwnerAcctId(), sid.getItemId());
/*     */       }
/* 369 */       return mptId;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/GetShareInfo.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */